<?php

namespace Tests;

use App\Models\User\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected $token;

    /** @var User $user */
    protected $user;

    protected function setUp(): void
    {
        parent::setUp();
        $this->user = factory(User::class)->create();
    }

    public function getAuthHeader($username = 'user123', $password = 'user1234') {
        $response = $this->post(
            'wx/auth/login', ['username' => $username, 'password' => $password]
        );
        $token = $response->getOriginalContent()['data']['token'] ?? '';
        return ['Authorization' => "Bearer {$token}"];
    }

    public function assertLitemallApiGet($uri) {
        return $this->assertLitemallApi($uri);
    }

    public function  assertLitemallApiPost($uri, $data = []) {
        return $this->assertLitemallApi($uri, 'post', $data);
    }

    public function assertLitemallApi($uri, $method = 'get', $data = []) {
        if ($method == 'get') {
            $response = $this->get($uri, $this->getAuthHeader());
            $list = $response->getOriginalContent();
        } else {
            $response = $this->post($uri,$data, $this->getAuthHeader());
            $list = $response->getOriginalContent();
        }
        $content = $response->getContent();
        //dd($content);
        $content = json_decode($content, true);
        $response->assertJson($content);
    }
}
